package com.fly.sms;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;

import com.fly.sms.progress.ThreadProcess;
import com.fly.sms.service.SmsService;
import com.fly.sms.util.StringUtil;

public class SmsSendLogActivity extends CommonActivity implements
		View.OnClickListener, OnDateSetListener, OnCheckedChangeListener,OnFocusChangeListener{
	List<Map<String, Object>> list;
	View closeBtn;
	RadioGroup smsSendState;
	String State;
	EditText Name;
	EditText StartTime;
	EditText EndTime;
	Button search;
	String dateBtnClick = "StartTime";

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.smssendlog);
		closeBtn = findViewById(R.id.closeBtn);
		smsSendState = (RadioGroup) findViewById(R.id.smsSendState);
		Name = (EditText) findViewById(R.id.Name);
		StartTime = (EditText) findViewById(R.id.StartTime);
		EndTime = (EditText) findViewById(R.id.EndTime);
		search = (Button) findViewById(R.id.search);
		closeBtn.setOnClickListener(this);
		smsSendState.setOnCheckedChangeListener(this);
		//StartTime.setOnClickListener(this);
		//StartTime.setOnTouchListener(this);
		StartTime.setOnFocusChangeListener(this);
		EndTime.setOnFocusChangeListener(this);
		//EndTime.setOnTouchListener(this);
		//EndTime.setOnClickListener(this);
		search.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		Calendar d = Calendar.getInstance(Locale.CHINA);
		d.setTime(new Date());
		switch (v.getId()) {
		case R.id.closeBtn:
			finish();
			break;
		/*case R.id.StartTime:
			dateBtnClick = "StartTime";
			new DatePickerDialog(this, this, d.get(Calendar.YEAR), d
					.get(Calendar.MONTH) + 1, d.get(Calendar.DAY_OF_MONTH))
					.show();
			break;
		case R.id.EndTime:
			dateBtnClick = "EndTime";
			new DatePickerDialog(this, this, d.get(Calendar.YEAR), d
					.get(Calendar.MONTH) + 1, d.get(Calendar.DAY_OF_MONTH))
					.show();
			break;*/
		case R.id.search:
			if (StringUtil.isEmpty(State)) {
				State = "sending";
			}
			if (StringUtil.isEmpty(StartTime.getText().toString())) {
				showMsg(getText(R.string.StartTime));
				StartTime.setFocusable(true);
				StartTime.setFocusableInTouchMode(true);
				StartTime.requestFocus();
				return;
			}
			if (StringUtil.isEmpty(EndTime.getText().toString())) {
				showMsg(getText(R.string.EndTime));
				EndTime.setFocusable(true);
				EndTime.setFocusableInTouchMode(true);
				EndTime.requestFocus();
				return;
			}
			new ThreadProcess(this, new ThreadProcess.onProcess() {
				@Override
				public void onStart() {
					list = SmsService.searchSendLog(State, StartTime.getText()
							.toString(), EndTime.getText().toString(), Name
							.getText().toString(), getCustomerId(), pNo, pSize);
				}

				@Override
				public void onFinished() {
					if (list == null) {
						context.showMsg(context.getText(R.string.nodata));
					} else {
						context.set("list", list);
						context.set("State", State);
						context
								.set("StartTime", StartTime.getText()
										.toString());
						context.set("EndTime", EndTime.getText().toString());
						context.set("Name", Name.getText().toString());
						forward(SmsSendLogListActivity.class);
						// finish();
					}
				}
			}).startProgress();

			break;
		}
	}

	@Override
	public void onDateSet(DatePicker view, int year, int monthOfYear,
			int dayOfMonth) {
		if ("StartTime".equals(dateBtnClick)) {
			StartTime.setText(Integer.toString(year) + "-"
					+ Integer.toString(monthOfYear+1) + "-"
					+ Integer.toString(dayOfMonth));
		} else if ("EndTime".equals(dateBtnClick)) {
			EndTime.setText(Integer.toString(year) + "-"
					+ Integer.toString(monthOfYear+1) + "-"
					+ Integer.toString(dayOfMonth));
		}
	}

	@Override
	public void onCheckedChanged(RadioGroup group, int checkedId) {
		switch (checkedId) {
		case R.id.smsSendingState:
			State = "sending";
			break;
		case R.id.smsSendedState:
			State = "sended";
			break;
		}
	}

	@Override
	public void onFocusChange(View v, boolean hasFocus) {
		Calendar d = Calendar.getInstance(Locale.CHINA);
		d.setTime(new Date());
		if(hasFocus){
			switch (v.getId()) {
			case R.id.StartTime:
				dateBtnClick = "StartTime";
				new DatePickerDialog(this, this, d.get(Calendar.YEAR), d
						.get(Calendar.MONTH) , d.get(Calendar.DAY_OF_MONTH))
						.show();
				break;
			case R.id.EndTime:
				dateBtnClick = "EndTime";
				new DatePickerDialog(this, this, d.get(Calendar.YEAR), d
						.get(Calendar.MONTH) , d.get(Calendar.DAY_OF_MONTH))
						.show();
				break;
			}
		}
	}
}